package com.bonc.ioc.service.impl;

import com.bonc.ioc.base.page.PageData;
import com.bonc.ioc.base.page.PageResult;
import com.bonc.ioc.base.service.BaseService;
import com.bonc.ioc.dao.KnowledgesMapper;
import com.bonc.ioc.dao.TopicMapper;
import com.bonc.ioc.service.KnowledgesService;
import com.bonc.ioc.vo.KnowledgesVo;
import com.bonc.ioc.vo.TopicKnowledge;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindException;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @description:
 * @author: wangqinglin
 * @date: 2021/3/18 11:06
 * @change : 2021/3/18 11:06 by q8541q@163.com  for init
 */
@Service
public class KnowledgesServiceImpl extends BaseService implements KnowledgesService {

    @Resource
    private KnowledgesMapper knowledgesMapper;

    @Autowired
    private TopicMapper topicMapper;

    @Override
    @Transactional
    public int deleteByPrimaryKey(String id) {

        //查询出所有专题和KnowledgeList
        List<TopicKnowledge> topicKnowledges = topicMapper.selectAllTopicKnowledge();
        for (TopicKnowledge topicKnowledge : topicKnowledges) {
            String knowledgeList = topicKnowledge.getKnowledgeList();
            String[] splits = new String[0];
            if (!StringUtils.isEmpty(knowledgeList)){
                splits = knowledgeList.split(",");
            }
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < splits.length; i++) {
                if (!splits[i].equals(id)){
                    if (i < splits.length-1){
                        stringBuilder.append(splits[i]+",");
                    }else {
                        stringBuilder.append(splits[i]);
                    }
                }
            }
            topicKnowledge.setKnowledgeList(String.valueOf(stringBuilder));
        }
        System.out.println(topicKnowledges);

        //删除专题对应的科研软件
        topicMapper.updateAllTopicKnowledge(topicKnowledges);

        //删除科研数据
        return knowledgesMapper.delete(id);
    }

    @Override
    public int save(KnowledgesVo record) {
        if (record.getId() != null) {
            fillModifyTimeInfo(record);
            return knowledgesMapper.update(record);
        } else {
            fillBasicTimeInfo(record);
            fillModifyTimeInfo(record);
            return knowledgesMapper.save(record);
        }
    }

    @Override
    public KnowledgesVo selectByPrimaryKey(Long id) {
        return knowledgesMapper.selectByPrimaryKey(id);
    }


    @Override
    public PageResult selectStandardInfoByPage(PageData pageData) throws BindException {
        getPageParams();
        PageResult pageResult = new PageResult(pageData, knowledgesMapper.selectByPage(pageData));
        return pageResult;
    }

    private void fillModifyTimeInfo(KnowledgesVo record) {
        record.setModifyTime(new Date());
    }

    private void fillBasicTimeInfo(KnowledgesVo record) {
        record.setCreateTime(new Date());
    }

    @Override
    public List<Map> getKnowledgeListInfos() {
        return knowledgesMapper.getKnowledgeListInfos();
    }
}
